package com.yinxin.thread.state;

import com.yinxin.util.ThreadUtil;

/**
 * @author yinxin
 * @Title:
 * @Package
 * @Description: 观测线程状态
 * @date 2022/1/815:15
 */
public class TestState {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                ThreadUtil.sleep(1000);//TIMED_WAITING
            }
            System.out.println("///////");
        });
        //观察状态
        Thread.State state = thread.getState();
        System.out.println(state);//NEW

        //观察启动后
        thread.start();//启动线程
        System.out.println(thread.getState());//RUNNABLE

        while (state!=Thread.State.TERMINATED){
            //只要线程不终止，就会一直输出状态
            ThreadUtil.sleep(100);
            Thread.State threadState = thread.getState();//TERMINATED
            System.out.println(threadState);
        }

    }
}
